﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using EHTWebsite.Code;
/// <summary>
/// Author: toanvt
/// CreatedDate: 12/12/2011
/// Description: Insert, Update Danh Mục Sản Phẩm
/// </summary>
public partial class Administrator_AdminProductCategoryIU : System.Web.UI.Page
{
    public Int32 danhMucID = HangSo.DEFAULT_ID;
    private String message = "";
    private DataTable dtProductCategorys = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Int32 totalRecord = 0;
            dtProductCategorys = DanhMucSanPhamProvider.GetDanhMucSanPhamsForBackEnd(-1, 1, 100000, out totalRecord).Tables[0];
            CreateDDLCategory(-1, 0);
            this.ddlChuyenMucCha.Items.Insert(0, new ListItem("----Danh mục sản phẩm----", "-1"));

            if (Request.QueryString["DanhMucID"] != null)
            {
                try
                {
                    danhMucID = Int32.Parse(Request.QueryString["DanhMucID"].ToString());
                }
                catch (Exception)
                {
                    danhMucID = -1;
                }
            }

            if (Request.QueryString["Message"] != null)
            {
                try
                {
                    message = Request.QueryString["Message"].ToString();
                }
                catch (Exception)
                {
                    message = "";
                }
            }

            //hiển thị Message nếu có giá trị Message gửi sang từ màn hình nguồn
            this.lbMessage.Text = message;
            this.lbMessage.CssClass = "BlueCorlor";

            if (danhMucID == -1)
            {
                ViewState["action"] = HangSo.ACTION_ADD;
                this.lbAdminCategory.Text = "Thêm mới Danh Mục Sản Phẩm";
                
            }
            else
            {
                DanhMucSanPham theDanhMucSanPham = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(danhMucID);

                if (theDanhMucSanPham == null)
                    Response.Redirect("~/Administrator/AdminProductCategorys.aspx");
                else
                {
                    this.txtTenDanhMucVN.Text = theDanhMucSanPham.TenDanhMucVN;
                    this.txtTenDanhMucOther.Text = theDanhMucSanPham.TenDanhMucOther;
                    this.txtSapXep.Text = theDanhMucSanPham.SapXep.ToString();
                    this.txtSoLuongTruyCap.Text = theDanhMucSanPham.LuongTruyCap.ToString();
                    this.ddlChuyenMucCha.SelectedValue = theDanhMucSanPham.ChuyenMucChaID.ToString();
                    this.ckPublish.Checked = theDanhMucSanPham.Publish;
                    ViewState["DanhMucID"] = theDanhMucSanPham.DanhMucID;
                    ViewState["DeletedFlag"] = theDanhMucSanPham.DeletedFlag.ToString();
                    ViewState["action"] = HangSo.ACTION_UPDATE;
                    this.lbAdminCategory.Text = "Sửa Danh Mục Sản Phẩm";
                }
            }

            // ẩn Validation cho các Control Other
            if ( HangSo.USING_MULTIL_LANGUAGE == false)
            {
                this.rvTenDanhMucOther.Enabled =false;
            }
        }
    }

    /// <summary>
    /// Author: toanvt
    /// CreatedDate: 12/12/2011
    /// Description: Kiểm tra xác thực dữ liệu nhập vào
    /// </summary>
    /// <returns>Trạng thái hợp lệ hay không</returns>
    private Boolean ValidateData()
    {
        this.lbMessage.Text = "";
        this.lbMessage.CssClass = "";

        String message = "";
        Boolean isSuccess = true;

        try
        {

        }
        catch (Exception)
        {

        }

        if (!isSuccess)
            ltrMessage.Text = message;

        return isSuccess;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Int32 danhMucIDOut = -1;
        String action = ViewState["action"].ToString();

        if (!ValidateData())
            return;

        DanhMucSanPham theDanhSachSanPham = new DanhMucSanPham();
        theDanhSachSanPham.TenDanhMucVN = this.txtTenDanhMucVN.Text.Trim();
        theDanhSachSanPham.TenDanhMucOther = this.txtTenDanhMucOther.Text.Trim();
        theDanhSachSanPham.Publish = this.ckPublish.Checked;
        try
        {
            theDanhSachSanPham.SapXep = Int32.Parse(this.txtSapXep.Text);
        }
        catch (Exception)
        {
            theDanhSachSanPham.SapXep = 1;
        }

        try
        {
            theDanhSachSanPham.LuongTruyCap = Int32.Parse(this.txtSoLuongTruyCap.Text.Trim());
        }
        catch (Exception ex)
        {
            theDanhSachSanPham.LuongTruyCap = 0;
        }
        try
        {            
            theDanhSachSanPham.ChuyenMucChaID = Int32.Parse(this.ddlChuyenMucCha.SelectedValue.ToString());
        }
        catch(Exception ex)
        { 
        }
        if (action == HangSo.ACTION_ADD)
        {
            theDanhSachSanPham.DanhMucID = -1;
            theDanhSachSanPham.DeletedFlag = false;
            danhMucIDOut = DanhMucSanPhamProvider.IUDanhMucSanPham(theDanhSachSanPham, true);
        }
        else
        {
            theDanhSachSanPham.DanhMucID = Int32.Parse(ViewState["DanhMucID"].ToString());
            theDanhSachSanPham.DeletedFlag = Boolean.Parse(ViewState["DeletedFlag"].ToString());
            danhMucIDOut = DanhMucSanPhamProvider.IUDanhMucSanPham(theDanhSachSanPham, false);
        }

        Response.Redirect("~/Administrator/AdminProductCategorys.aspx?DanhMucID=" + theDanhSachSanPham.ChuyenMucChaID.ToString());
    }
    /// <summary>
    /// Author:tungvu90
    /// Create Date:07/02/2012
    /// Description:tạo DropDownList bằng đệ quy
    /// </summary>
    /// <param name="ChuyenMucChaID"></param>
    /// <param name="level"></param>
    private void CreateDDLCategory(Int32 ChuyenMucChaID, Int32 level)
    {
        String preString = "";

        for (Int32 i = 0; i < level; ++i)
        {
            preString += "--";
        }

        DataRow[] drCategorysSub = dtProductCategorys.Select("ChuyenMucChaID = " + ChuyenMucChaID.ToString());
        Int32 amountOfCategorySub = drCategorysSub.Count();

        //Điểm dừng đệ quy
        if (amountOfCategorySub == 0)
            return;

        for (Int32 i = 0; i < amountOfCategorySub; ++i)
        {
            Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucID"].ToString());
            ListItem li = new ListItem(preString + drCategorysSub[i]["TenDanhMucVN"].ToString(), drCategorysSub[i]["DanhMucID"].ToString());
            this.ddlChuyenMucCha.Items.Add(li);
            CreateDDLCategory(subCategoryID, level + 1);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Administrator/AdminProductCategorys.aspx?DanhMucID=" + ddlChuyenMucCha.SelectedValue.ToString());
    }
    /// <summary>
    /// Author:tungvu90
    /// Create Date:08/02/2012
    /// Description:thực hiện nút áp dụng danh sách sản phẩm
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnApply_Click(object sender, EventArgs e)
    {
        Int32 danhMucIDOut = -1;
        String action = ViewState["action"].ToString();

        if (!ValidateData())
            return;

        DanhMucSanPham theDanhSachSanPham = new DanhMucSanPham();
        theDanhSachSanPham.TenDanhMucVN = this.txtTenDanhMucVN.Text.Trim();
        theDanhSachSanPham.TenDanhMucOther = this.txtTenDanhMucOther.Text.Trim();
        theDanhSachSanPham.Publish = this.ckPublish.Checked;

        try
        {
            theDanhSachSanPham.SapXep = Int32.Parse(this.txtSapXep.Text);
        }
        catch (Exception)
        {
            theDanhSachSanPham.SapXep = 1;
        }

        try
        {
            theDanhSachSanPham.LuongTruyCap = Int32.Parse(this.txtSoLuongTruyCap.Text.Trim());
        }
        catch (Exception ex)
        {
            theDanhSachSanPham.LuongTruyCap = 0;
        }

        try
        {
            theDanhSachSanPham.ChuyenMucChaID = Int32.Parse(this.ddlChuyenMucCha.SelectedValue.ToString());
        }
        catch (Exception ex)
        {
        }

        if (action == HangSo.ACTION_ADD)
        {
            theDanhSachSanPham.DanhMucID = -1;
            theDanhSachSanPham.DeletedFlag = false;
            danhMucIDOut = DanhMucSanPhamProvider.IUDanhMucSanPham(theDanhSachSanPham, true);
        }
        else
        {
            theDanhSachSanPham.DanhMucID = Int32.Parse(ViewState["DanhMucID"].ToString());
            theDanhSachSanPham.DeletedFlag = Boolean.Parse(ViewState["DeletedFlag"].ToString());
            danhMucIDOut = DanhMucSanPhamProvider.IUDanhMucSanPham(theDanhSachSanPham, false);
        }

        Response.Redirect("AdminProductCategoryIU.aspx?DanhMucID=" + danhMucIDOut.ToString() + "&Message=Lưu thành công");
    }
}
